package com.bawei.fuxi.ui.home;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.fuxi.R;
import com.bawei.fuxi.base.mvp.BaseFragment;
import com.bawei.fuxi.base.mvp.BasePresenter;
import com.bawei.fuxi.base.utils.GlideUtils;
import com.bawei.fuxi.entity.BannerEntity;
import com.bawei.fuxi.entity.VideoEntity;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 2．视频fragment：
 （1）上方显示轮播图
 （2）自定义view完成搜索布局
 （3）实现搜索功能
 （4）请求视频数据瀑布流展示
 （5）自定义分割线
 （6）完成上拉下拉加载更多
 （7）点击item跳转到视频详情页面
 （8）视频能播放和暂停
 （9）视频能横竖屏切换
 （10）能够发送弹幕
 */

public class HomeFragment extends BaseFragment<HomeContract.IHomeView,HomePresenter> implements HomeContract.IHomeView {
    private EditText et;
    private Banner banner;
    private SmartRefreshLayout smart;
    private RecyclerView rv;
    private VideoAdapter mVideoAdapter;

    private int page = 1;


    @Override
    public int bindLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        et = (EditText) findViewById(R.id.et);
        banner = (Banner) findViewById(R.id.banner);
        smart = (SmartRefreshLayout) findViewById(R.id.smart);
        rv = (RecyclerView) findViewById(R.id.rv);
        mVideoAdapter = new VideoAdapter(R.layout.item_video_home);
        rv.setAdapter(mVideoAdapter);
        rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        //上拉下拉
        smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.video(page,10);
                smart.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mVideoAdapter.getData().clear();
                mPresenter.video(page,10);
                smart.finishRefresh();

            }
        });
        //搜索
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.search(s.toString());

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //点击item跳转到视频播放页面
        mVideoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                ARouter.getInstance().build("/app/video")
                        .withSerializable("video",mVideoAdapter.getData().get(position))
                        .navigation();
            }
        });
    }


    @Override
    public HomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    public void initData() {
        mPresenter.banner();
        mPresenter.video(1,10);
    }

    @Override
    public void showVideo(VideoEntity videoEntity) {
        if(videoEntity != null && videoEntity.getCode() == 200){
            mVideoAdapter.getData().addAll(videoEntity.getData());
            mVideoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showSearch(VideoEntity videoEntity) {
        if(videoEntity != null && videoEntity.getCode() == 200){
            mVideoAdapter.getData().clear();//清除数据
            mVideoAdapter.getData().addAll(videoEntity.getData());
            mVideoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showBanner(BannerEntity bannerEntity) {
        if(bannerEntity != null && bannerEntity.getCode() == 200){
            List<BannerEntity.DataBean> data = bannerEntity.getData();
            List<String> images = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (BannerEntity.DataBean datum : data) {
                images.add(datum.getImagePath());
                titles.add(datum.getDesc());
            }
            //轮播图
            banner.setImages(images);
            banner.setBannerTitles(titles);
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    GlideUtils.getInstance().loadImage(context, (String) path,imageView);
                }
            });
            banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);//数字+标题
            banner.start();
        }
    }
}